$(document).ready(function(){
//	alert("hi");
	/*FOR DAMAGE:

IF DAMAGE EXCEEDS : 2% AND ACTUAL DAMAGE IS SAY 2.50%
THEN ABOVE CALCULATIONS IS 
BASIC 2% LESS ACTUAL 2.5% SO FIGURE IS 0.50%
SO CLAIM WILL BE DONE AS 0.50%/2 i.e 0.25%

FOR SAND/SILICA:
IF SAND/SILICA EXCEEDS: 2% AND ACTUAL SAND /SILICA IS SAY 2.50%
THEN ABOVE CALCULATIONS IS 
BASIC 2%- 2.5% ACTUAL SO FIGURES COEMS TO 0.50%
AND ACTUAL CLAIM WILL BE ALSO 0.50% AND IF SAND EXCEEDS 6% THAN CLAIM WILL BE: DOUBLE
i.e: anything above 6% sand will be directly added in claim
FOR EXAMPLE:
IF SAND SILICA IS FOR EXAMPLE 7.5%
THAN CALCULATION WILL BE
BASIC2%-7.5%= 5.5% PLUS AS 7.5% ECXEEDS 6% ADDITIONAL 1.5% WILL BE ADDED OVER ABOVE 5.5%
TOTAL CLAIM WILL BE : 5.5%+1.5% = 7%

FOR MOISTURE:
IF MOISTURE EXCEEDS 10% AND ACTUAL MOISTURE IS SAY 12.50%
THEN ABOVE CALCULATIONS IS 
BASIC 10%-12.50% THAN ACTUAL CLAIM IS 2.5%
AND IF MOISTURE EXCEEDS 15% THAN CLAIM WILL E DOUBLE i.e: anything above 15% will be directly added in claim
FOR EXAMPLE:
IF MOISTURE IS 16.50%
THAN CALCULATION WILL BE
BASIC 10%-16.50% = 6.5% PLUS AS 16.5% EXCEEDS 15% ADDITIONAL 1.5% WILL BE ADDED OVER AND ABOVE 6.5%
TOTAL CLAIM WILL BE : 6.5%+ 1.5%= 8%*/
	 
	
	
	$('#sandActual, #sandDiff, #priceSubSand, #moistureActual,#moistureDiff,#priceSubMoisture, #damageActual, #damageDiff, #priceSubDamage, #costUnload, #discount, #paidAdvance, #totalRebate').bind('keypress', function (e) {
        return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && e.which != 46) ? false : true;
    });
	
	 $('#calculate').bind('click',function(){
		// BenchMark values set by Admin
	 var silica = $('#sandActual').val();
	var damage = $('#damageActual').val();
	var moisture = $('#moistureActual').val();
	console.log(silica)
	
	// Values from DB
	
	var benchmarkSilica = "2";
	var benchmarkMoisture = "10";
	var benchmarkDamage = "2";
	
	
	// Parameters calculations
	
	var silicadiff = silica-benchmarkSilica;
	if(silicadiff<0)
	{
		silicadiff=0;
	}
	console.log("val ===>"+silicadiff);
	if(silica>6)
	{
		var excessSilica = silica-6;
		silicadiff = silicadiff + excessSilica;
	}
	
	var damagediff = damage-benchmarkDamage;
		damagediff = damagediff/2;
	if(damagediff<0)
	{
		damagediff=0;
	}
	var moisturediff = moisture-benchmarkMoisture;
	if(moisture>15)
 	{
		var excessMoisture = moisture-15;
		moisturediff = moisturediff + excessMoisture;
	}
	if(moisturediff<0)
	{
		moisturediff=0;
	}
	$('#sandDiff').val(silicadiff);
	$('#moistureDiff').val(moisturediff);
	$('#damageDiff').val(damagediff);
	
	//Price Calculations
	
	var weight = $('#weight').val();
	var rate = $('#rate').val();
	var gross = weight*rate;
	
	$('#gross').val(gross);
	
	var priceSubSand = (silicadiff*gross)/100;
	var priceSubMoisture = (moisturediff*gross)/100;
	var priceSubDamage = (damagediff*gross)/100;
	
	$('#priceSubSand').val(priceSubSand);
	$('#priceSubMoisture').val(priceSubMoisture);
	$('#priceSubDamage').val(priceSubDamage);
	
	// Other Calculations
	
	var unloading = $('#costUnload').val();
	var cashdisc = $('#discount').val();
	var advance = $('#paidAdvance').val();
	var addition = + unloading+ +cashdisc+ +advance;
	console.log(addition);
	var totalRebate =  + priceSubSand + priceSubMoisture + priceSubDamage + +unloading+ +cashdisc+ +advance;
	
	$('#totalRebate, #less').val(totalRebate);
	
	var dalali = $('#addDalali').val();
	
	var totalR = gross-(+dalali + +totalRebate);
	
	$('#totalR').val(totalR);
	 });
	 // href="mailto:?subject=Mail from Our Site&body"
	 $('#btnemail').bind('click',function() {
	 
	 var content = $('table tbody').html();
	 
     //   $('#form').attr('action',
     //                  'mailto:?subject=Mail from Our Site');
				$('#form').submit();	   
   //     alert(1);
    });
	 
	//alert($('table tbody').html());
});

	